package cn.xo68.demo.controller;

import cn.xo68.boot.qrcode.QrCodeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;


@Api(value = "二维码生成",tags = {"二维码生成"})
@Slf4j
@RestController
@RequestMapping
public class QrController {

    @Autowired
    private QrCodeGenerator qrCodeGenerator;


    @ApiOperation(value="生成二维码图片")
    @GetMapping(value = "/qr.png")
    public ResponseEntity<?> qr(@RequestParam(value = "content") String content) throws IOException {

        ClassPathResource classPathResource=new ClassPathResource("logo.jpg");
        BufferedImage logoImage = ImageIO.read(classPathResource.getInputStream());
        byte[] bytes=qrCodeGenerator.generateAndToBytes(content,logoImage);
        return ResponseEntity.status(HttpStatus.OK)
                .contentType(MediaType.IMAGE_PNG)
                .contentLength(bytes.length)
                .body(bytes);
    }
}
